//
//  BanDingBankCardViewController.m
//  gpos_app
//
//  Created by 崔航飞 on 2018/8/18.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "BanDingBankCardViewController.h"
#import "Masonry.h"
#import "NewBankCardViewController.h"
#import "GPosAPI.h"
#import "BanDingBankViewCell.h"
#import "XMRegisterViewController.h"
#import "SVProgressHUD.h"
#import "minerModel.h"
@interface BanDingBankCardViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) UIView *backgroundViews;
@property (nonatomic,strong) UITableView * tableView;
@property (nonatomic,strong)NSMutableArray * modelArr;
@end

@implementation BanDingBankCardViewController
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden=NO;
    //去掉返回的文字
    self.navigationController.navigationBar.topItem.title = @"";
    self.navigationItem.title=@"我的银行卡";
   
        [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
    
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    
}
-(void)viewDidLayoutSubviews{
    
    [super viewDidLayoutSubviews];
    self.tabBarController.tabBar.hidden = YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor whiteColor];
    self.navigationController.navigationBar.backgroundColor=[UIColor whiteColor];
    self.navigationController.navigationBar.tintColor=[UIColor blackColor];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(walkVCClick:) name:@"bangdingyinghangka" object:nil];
    [self dataLoad];
    
}

- (void)walkVCClick:(NSNotification *)noti

{
    
    [self dataLoad];
}
-(void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    
}
- (void)dataLoad{
    //requestGetBankCardWithParams
    int user = [[[NSUserDefaults standardUserDefaults] objectForKey:@"userId"] intValue];
    NSDictionary * params = @{@"userId" : @(user)};
    [GPosAPI requestGetBankCardWithParams:params andCallback:^(id obj) {
        NSLog(@"银行卡--%@",obj);
        if ( obj == nil ){
            
            [SVProgressHUD showInfoWithStatus:@"请求失败"];
            [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
            [SVProgressHUD setDefaultAnimationType:SVProgressHUDAnimationTypeFlat];
            
        }else {
            
            self.modelArr = [[NSMutableArray alloc] init];
            if ([[obj objectForKey:@"code"] intValue] == 200) {
                NSArray * arr = [[NSArray alloc] init];
                arr = [obj objectForKey:@"data"];
                for (NSDictionary * dic in arr) {
                    minerModel *model = [[minerModel alloc] init];
                
                    model.bankBranchName = [dic objectForKey:@"bankBranchName"];
                    model.bankCNFName = [dic objectForKey:@"bankCNFName"];
                    model.bankCardNumber = [dic objectForKey:@"bankCardNumber"];
                   
                    [self.modelArr addObject:model];
                }
                
                
                
                NSLog(@"self---%@",[obj objectForKey:@"data"]);
                
            }else if ([[obj objectForKey:@"code"] intValue] == 401 || [[obj objectForKey:@"code"] intValue] == 402) {
                UIStoryboard *mainStory = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
                [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"passWord"];
                //storyboard里的ID
                XMRegisterViewController *vc = [mainStory instantiateInitialViewController];
                vc.navigationController.navigationBarHidden=NO;
                //                    //设置当前窗口的根控制器
                
                [UIApplication sharedApplication].keyWindow.rootViewController=vc;
                [SVProgressHUD showInfoWithStatus:[obj objectForKey:@"message"]];
                [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
                [SVProgressHUD setDefaultAnimationType:SVProgressHUDAnimationTypeFlat];
                
            }else {
                [SVProgressHUD showErrorWithStatus:[obj objectForKey:@"message"]];
                [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
                [SVProgressHUD setDefaultAnimationType:SVProgressHUDAnimationTypeFlat];
                
            }
        }
        
        [self initUI];
        
    }];
    
    
    
    
}

-(void)initUI{
    
    self.backgroundViews=[[UIView alloc] init];
    self.backgroundViews.backgroundColor=[UIColor colorWithWhite:0.94 alpha:1];
    [self.view addSubview:self.backgroundViews];
    [self.backgroundViews mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.view.mas_top).offset(kNavbarHeight);
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(self.view.mas_right);
        make.height.equalTo(@(KmainScreenHeiht - kNavbarHeight - kBottomSafeHeight));
        
    }];
        
    self.tableView = [[UITableView alloc] init];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundColor=[UIColor colorWithWhite:0.94 alpha:1];
    [self.backgroundViews addSubview:self.tableView];
        [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.backgroundViews).with.offset(0);
            make.left.equalTo(self.backgroundViews).with.offset(0);
            make.right.equalTo(self.backgroundViews).with.offset(0);
            make.bottom.equalTo(self.backgroundViews).with.offset(0);
        }];
        
        UIView * footerView = [[UIView alloc] init];
//        footerView.backgroundColor = [UIColor colorWithRed:244/255.0f green:244/255.0f blue:244/255.0f alpha:1];
        footerView.frame = CGRectMake(0, 0, KmainScreenWidth, 30);
        self.tableView.tableFooterView = footerView;
    
//    UIBarButtonItem *rightBarItem = [[UIBarButtonItem alloc] initWithTitle:@"确认" style:UIBarButtonItemStylePlain target:self action:@selector(onClickedOKbtn)];
//    UIBarButtonItem *rightBarItem = [[UIBarButtonItem alloc] initWithTitle:@"确认" style:UIBarButtonItemStylePlain target:self action:@selector(onClickedOKbtn)];
    UIBarButtonItem *rightBarItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(onClickedOKbtn)];
    self.navigationItem.rightBarButtonItem = rightBarItem;
        
        
    //缺省图
    UIImageView * qstImage = [[UIImageView alloc] init];
    qstImage.image = [UIImage imageNamed:@"icon_zwyhk"];
    [self.backgroundViews addSubview:qstImage];
    [qstImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.backgroundViews);//
        make.top.equalTo(self.backgroundViews).with.offset(KmainScreenHeiht/4);
    }];
    UILabel * qstLabel = [[UILabel alloc] init];
    qstLabel.text = @"暂无银行卡";
    qstLabel.textColor = [UIColor grayColor];
    qstLabel.font = [UIFont systemFontOfSize:13.f];
    [self.backgroundViews addSubview:qstLabel];
    [qstLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(qstImage.mas_bottom).with.offset(5);
        make.centerX.equalTo(qstImage);
    }];
    
    if (self.modelArr.count == 0) {
        self.tableView.hidden = YES;
    }else {
        
        qstImage.hidden = YES;
        qstLabel.hidden = YES;
    }
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.modelArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{//rankingCell
    
    
    BanDingBankViewCell * cell = [[BanDingBankViewCell alloc] init];
    cell = [tableView dequeueReusableCellWithIdentifier:@"BanDingBankCell"];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"BanDingBankViewCell" owner:self options:nil] firstObject];
    }
    
    cell.BGView.layer.cornerRadius = 5;
    cell.BankTypeLabel.text = [self.modelArr[indexPath.row] bankCNFName];
    cell.BankDizhiLabel.text = [self.modelArr[indexPath.row] bankBranchName];
    NSString * number = [self.modelArr[indexPath.row] bankCardNumber];
    NSString * str = [number substringToIndex:4];
    NSString * twoStr = [number substringFromIndex:number.length-4];
    NSLog(@"str---%@, two--%@",str,twoStr);
    
    cell.CardNumberLabel.text = [NSString stringWithFormat:@"%@ *** *** %@",str,twoStr];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 130;
}
//绑定新的银行卡点击事件
-(void)onClickedOKbtn{
    
    NewBankCardViewController *vc=[[NewBankCardViewController alloc] init];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
